/**
 * Copyright © 2019 dream horse Info. Tech Ltd. All rights reserved.
 *
 * @Package: com.gitee.mybatis.fl.convert
 * @author: flying-cattle
 * @date: 2019年4月9日 下午8:15:25
 */
package com.fdy.code.util;

import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.fdy.code.entity.BasisInfo;
import com.fdy.code.entity.ConfigObj;
import com.fdy.code.entity.PropertyInfo;
import com.fdy.code.entity.ResultJson;
import com.fdy.code.enums.FreemarkerTemplateEnum;

/**
 * Copyright: Copyright (c) 2019
 *
 * <p>说明：  获取文件路径调用创建文件</P>
 *
 * @version: v3.0.0
 * @author: flying-cattle
 * <p>
 * Modification History:
 * Date         	Author          Version          Description
 * ---------------------------------------------------------------*
 * 2019年4月9日      		flying-cattle   v3.0.0           initialize
 */
public class Generator {
    //路径信息
    public static final String ENTITY = "entity";
    public static final String DAO = "dao";
    public static final String DAO_IMPL = "mapper";
    public static final String SERVICE = "service";
    public static final String SERVICE_IMPL = "serviceImpl";
    public static final String CONTROLLER = "controller";
    public static final String SWAGGER_CONFIG = "swaggerConfig";

    //①创建实体类
    public static String createEntity(String url, BasisInfo bi) {
        ResultJson entity = createEntity(url, bi,
                FreemarkerTemplateEnum.getTemplateType(bi.getConfigObj().getTemplateType()));
        return entity.getMessage();
    }

    public static ResultJson createEntity(String url, BasisInfo bi, FreemarkerTemplateEnum freemarkerTemplateEnum) {
        ConfigObj configObj = bi.getConfigObj();
        String fileUrl = getGeneratorFileUrl(url, configObj.getEntityPath(), bi.getEntityName(),
                ENTITY);
        String entity = freemarkerTemplateEnum.getFreemarkerInfo().getEntity();
        if (StrUtil.isNotEmpty(entity)) {
            return FreemarkerUtil.createFile(bi, entity, fileUrl);
        }
        ResultJson result = new ResultJson();
        result.setCode(-1);
        result.setMessage("创建" + bi.getEntityName() + "失败");
        return result;
    }

    //②创建DAO
    public static ResultJson createDao(String url, BasisInfo bi) {
        ConfigObj configObj = bi.getConfigObj();
        return createDao(url, bi, FreemarkerTemplateEnum.getTemplateType(configObj.getTemplateType()));
    }

    public static ResultJson createDao(String url, BasisInfo bi, FreemarkerTemplateEnum freemarkerTemplateEnum) {
        ConfigObj configObj = bi.getConfigObj();

        String fileUrl = getGeneratorFileUrl(url, configObj.getDaoPath(), bi.getEntityName(), DAO);
        String dao = freemarkerTemplateEnum.getFreemarkerInfo().getDao();
        if (StrUtil.isNotEmpty(dao)) {
            return FreemarkerUtil.createFile(bi, dao, fileUrl);
        }
        ResultJson result = new ResultJson();
        result.setCode(-1);
        result.setMessage("创建" + bi.getEntityName() + "dao失败");
        return result;
    }

    //③创建mapper配置文件
    public static ResultJson createDaoImpl(String url, BasisInfo bi) {
        ConfigObj configObj = bi.getConfigObj();
        return createDaoImpl(url, bi, FreemarkerTemplateEnum.getTemplateType(configObj.getTemplateType()));
    }

    public static ResultJson createDaoImpl(String url, BasisInfo bi, FreemarkerTemplateEnum freemarkerTemplateEnum) {
        ConfigObj configObj = bi.getConfigObj();
        String fileUrl = getGeneratorFileUrl(url, configObj.getDaoXmlPath(), bi.getEntityName(), DAO_IMPL);
        List<PropertyInfo> list = bi.getCis();
        String agile = "";
        for (PropertyInfo propertyInfo : list) {
            agile = agile + propertyInfo.getColumn() + ", ";
        }
        String mapper = freemarkerTemplateEnum.getFreemarkerInfo().getMapper();
        if (StrUtil.isNotEmpty(mapper)) {
            return FreemarkerUtil.createFile(bi, mapper, fileUrl);
        }
        ResultJson result = new ResultJson();
        result.setCode(-1);
        result.setMessage("创建" + bi.getEntityName() + "mapper失败");
        return result;
    }

    //④创建SERVICE
    public static ResultJson createService(String url, BasisInfo bi) {
        ConfigObj configObj = bi.getConfigObj();
        return createService(url, bi, FreemarkerTemplateEnum.getTemplateType(configObj.getTemplateType()));
    }

    public static ResultJson createService(String url, BasisInfo bi, FreemarkerTemplateEnum freemarkerTemplateEnum) {
        ConfigObj configObj = bi.getConfigObj();
        String fileUrl = getGeneratorFileUrl(url, configObj.getServicePath(), bi.getEntityName(), SERVICE);
        String service = freemarkerTemplateEnum.getFreemarkerInfo().getService();
        if (StrUtil.isNotEmpty(service)) {
            return FreemarkerUtil.createFile(bi, service, fileUrl);
        }
        ResultJson result = new ResultJson();
        result.setCode(-1);
        result.setMessage("创建" + bi.getEntityName() + "service失败");
        return result;
    }

    //⑤创建SERVICE_IMPL
    public static ResultJson createServiceImpl(String url, BasisInfo bi) {
        ConfigObj configObj = bi.getConfigObj();
        return createServiceImpl(url, bi, FreemarkerTemplateEnum.getTemplateType(configObj.getTemplateType()));
    }

    public static ResultJson createServiceImpl(String url, BasisInfo bi, FreemarkerTemplateEnum freemarkerTemplateEnum) {
        ConfigObj configObj = bi.getConfigObj();
        String fileUrl = getGeneratorFileUrl(url, configObj.getServiceImplPath(), bi.getEntityName(), SERVICE_IMPL);
        String serviceImpl = freemarkerTemplateEnum.getFreemarkerInfo().getServiceImpl();
        if (StrUtil.isNotEmpty(serviceImpl)) {
            return FreemarkerUtil.createFile(bi, serviceImpl, fileUrl);
        }
        ResultJson result = new ResultJson();
        result.setCode(-1);
        result.setMessage("创建" + bi.getEntityName() + "serviceImpl失败");
        return result;

    }

    //⑥创建CONTROLLER
    public static ResultJson createController(String url, BasisInfo bi) {
        ConfigObj configObj = bi.getConfigObj();
        return createController(url, bi, FreemarkerTemplateEnum.getTemplateType(configObj.getTemplateType()));
    }

    public static ResultJson createController(String url, BasisInfo bi, FreemarkerTemplateEnum freemarkerTemplateEnum) {
        ConfigObj configObj = bi.getConfigObj();
        String fileUrl = getGeneratorFileUrl(url, configObj.getControllerPath(), bi.getEntityName(), CONTROLLER);

        String controller = freemarkerTemplateEnum.getFreemarkerInfo().getController();
        if (StrUtil.isNotEmpty(controller)) {
            return FreemarkerUtil.createFile(bi, controller, fileUrl);
        }
        ResultJson result = new ResultJson();
        result.setCode(-1);
        result.setMessage("创建" + bi.getEntityName() + "controller失败");
        return result;
    }


    //⑧创建CONTROLLER
    public static ResultJson createSwaggerConfig(String url, BasisInfo bi) {
        return createSwaggerConfig(url, bi, FreemarkerTemplateEnum.DEFAULT_TEMPLATE);
    }

    public static ResultJson createSwaggerConfig(String url, BasisInfo bi, FreemarkerTemplateEnum freemarkerTemplateEnum) {
        ConfigObj configObj = bi.getConfigObj();
        String fileUrl = getGeneratorFileUrl(url, configObj.getSwaggerConfigPath(), "Swagger", SWAGGER_CONFIG);
//        return FreemarkerUtil.createFile(bi, "SwaggerConfig.ftl", fileUrl);
        String swaggerConfig = freemarkerTemplateEnum.getFreemarkerInfo().getSwaggerConfig();
        if (StrUtil.isNotEmpty(swaggerConfig)) {
            return FreemarkerUtil.createFile(bi, swaggerConfig, fileUrl);
        }
        ResultJson result = new ResultJson();
        result.setCode(-1);
        result.setMessage("创建" + bi.getEntityName() + "swaggerConfig失败");
        return result;
    }


    public static String getGeneratorFileUrl(String url, String packageUrl, String entityName, String type) {
        return getGeneratorFileUrl(url,packageUrl,entityName,type,"Mapper");
    }
    /**
     * 生成文件路径和名字
     *
     * @param url
     * @param packageUrl
     * @param entityName
     * @param type
     * @return
     */
    public static String getGeneratorFileUrl(String url, String packageUrl, String entityName, String type,
                                             String subName) {
        if (type.equals("entity")) {
            return url + pageToUrl(packageUrl) + entityName + ".java";
        }else if (type.equals("dao") && StrUtil.isEmpty(subName)) {
            return url + pageToUrl(packageUrl) + entityName + "Dao.java";
        } else if (type.equals("dao") && StrUtil.isNotEmpty(subName)) {
            return url + pageToUrl(packageUrl) + entityName + subName+".java";
        } else if (type.equals("mapper")) {
            return url + pageToUrl(packageUrl) + entityName + "Mapper.xml";
        } else if (type.equals("service")) {
            return url + pageToUrl(packageUrl) + entityName + "Service.java";
        } else if (type.equals("serviceImpl")) {
            return url + pageToUrl(packageUrl) + entityName + "ServiceImpl.java";
        } else if (type.equals("controller")) {
            return url + pageToUrl(packageUrl) + entityName + "Controller.java";
        } else if (type.equals("swaggerConfig")) {
            return url + pageToUrl(packageUrl) + entityName + "Config.java";
        }
        return null;
    }

    public static String pageToUrl(String url) {
        return url.replace(".", "/") + "/";
    }

}
